import requests
from bs4 import BeautifulSoup

def get_baidu_word_count():
    try:
        # 发送 HTTP 请求获取百度页面内容
        url = 'https://www.baidu.com'
        response = requests.get(url)
        # 设置响应内容的编码为网页实际编码
        response.encoding = response.apparent_encoding
        # 检查请求是否成功
        response.raise_for_status()

        # 使用 BeautifulSoup 解析 HTML 内容
        soup = BeautifulSoup(response.text, 'html.parser')
        # 提取页面中的所有文字
        text = soup.get_text()
        # 去除多余的空白字符
        text = ''.join(text.split())
        # 统计文字数量
        word_count = len(text)
        return word_count
    except requests.RequestException as e:
        print(f"请求出错: {e}")
    except Exception as e:
        print(f"发生其他错误: {e}")
    return 0

if __name__ == "__main__":
    word_count = get_baidu_word_count()
    print(f"百度页面的文字数量为: {word_count}")